<template>
	<gradient-background>
		<view class="container">
			<view class="content">
				<!-- 页面标题 -->
				<view class="page-header">
					<view class="back-button" @tap="goBack">
						<text class="back-icon">←</text>
					</view>
					<view class="header-info">
						<text class="tool-icon">🔍</text>
						<text class="tool-title">寻人占卜</text>
						<text class="tool-subtitle">寻人占卜和方位指引</text>
					</view>
				</view>
				
				<!-- 参数输入区域 -->
				<app-card>
					<view class="input-section">
						<text class="section-title">寻找信息</text>
						
						<view class="form-item">
							<text class="form-label">要寻找的人</text>
							<input 
								v-model="params.personName" 
								placeholder="请输入要寻找人的姓名"
								class="input-field"
							/>
						</view>
						
						<view class="form-item">
							<text class="form-label">关系</text>
							<picker 
								mode="selector" 
								:range="relationTypes" 
								:value="relationIndex"
								@change="onRelationChange"
								class="relation-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayRelation }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">失联时间</text>
							<picker 
								mode="date" 
								:value="params.lostDate" 
								@change="onLostDateChange"
								class="date-picker"
							>
								<view class="picker-display">
									<text class="picker-text">{{ displayLostDate }}</text>
									<text class="picker-arrow">→</text>
								</view>
							</picker>
						</view>
						
						<view class="form-item">
							<text class="form-label">最后出现地点</text>
							<textarea 
								v-model="params.lastLocation" 
								placeholder="请描述最后出现的地点或区域"
								class="location-input"
								maxlength="100"
							></textarea>
						</view>
						
						<view class="form-item">
							<text class="form-label">其他信息</text>
							<textarea 
								v-model="params.otherInfo" 
								placeholder="请描述其他相关信息（如年龄、特征等）"
								class="info-input"
								maxlength="200"
							></textarea>
						</view>
					</view>
				</app-card>
				
				<!-- 查询按钮 -->
				<view class="action-section">
					<app-button 
						:text="loading ? '占卜中...' : '开始占卜'" 
						type="primary"
						@tap="queryFindPerson"
						:disabled="!canQuery || loading"
						:loading="loading"
					></app-button>
				</view>
				
				<!-- 结果显示区域 -->
				<app-card v-if="findPersonResult">
					<view class="result-section">
						<text class="section-title">寻人分析</text>
						
						<view class="find-person-analysis">
							<view class="overall-result">
								<view class="result-score">
									<view class="score-circle">
										<text class="score-text">{{ findPersonResult.overall.score }}</text>
										<text class="score-unit">分</text>
									</view>
									<text class="score-level">{{ findPersonResult.overall.level }}</text>
								</view>
								<view class="result-desc">
									<text class="desc-text">{{ findPersonResult.overall.description }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 方位指引 -->
				<app-card v-if="findPersonResult && findPersonResult.direction">
					<view class="direction-section">
						<text class="section-title">方位指引</text>
						
						<view class="direction-analysis">
							<view class="direction-item primary">
								<text class="direction-label">主要方位</text>
								<text class="direction-value">{{ findPersonResult.direction.primary }}</text>
								<text class="direction-desc">重点搜寻方向</text>
							</view>
							
							<view class="direction-item secondary">
								<text class="direction-label">次要方位</text>
								<text class="direction-value">{{ findPersonResult.direction.secondary }}</text>
								<text class="direction-desc">备选搜寻方向</text>
							</view>
							
							<view class="direction-item avoid">
								<text class="direction-label">避开方位</text>
								<text class="direction-value">{{ findPersonResult.direction.avoid }}</text>
								<text class="direction-desc">暂时避开此方向</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 时间分析 -->
				<app-card v-if="findPersonResult && findPersonResult.timeFrame">
					<view class="time-section">
						<text class="section-title">时间分析</text>
						
						<view class="time-analysis">
							<view class="time-item best">
								<text class="time-label">最佳时机</text>
								<text class="time-value">{{ findPersonResult.timeFrame.best }}</text>
							</view>
							<view class="time-item possible">
								<text class="time-label">可能时机</text>
								<text class="time-value">{{ findPersonResult.timeFrame.possible }}</text>
							</view>
							<view class="time-item difficult">
								<text class="time-label">困难时期</text>
								<text class="time-value">{{ findPersonResult.timeFrame.difficult }}</text>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 位置分析 -->
				<app-card v-if="findPersonResult && findPersonResult.location">
					<view class="location-section">
						<text class="section-title">位置分析</text>
						
						<view class="location-analysis">
							<view class="location-info">
								<text class="location-type">{{ findPersonResult.location.type }}</text>
								<text class="location-description">{{ findPersonResult.location.description }}</text>
							</view>
							
							<view class="location-keywords">
								<text class="keywords-label">关键地点：</text>
								<view class="keywords-list">
									<text 
										v-for="keyword in findPersonResult.location.keywords" 
										:key="keyword"
										class="keyword-item"
									>{{ keyword }}</text>
								</view>
							</view>
						</view>
					</view>
				</app-card>
				
				<!-- 建议 -->
				<app-card v-if="findPersonResult && findPersonResult.suggestions">
					<view class="suggestions-section">
						<text class="section-title">寻找建议</text>
						
						<view class="suggestions-list">
							<view 
								v-for="(suggestion, index) in findPersonResult.suggestions" 
								:key="index"
								class="suggestion-item"
							>
								<view class="suggestion-icon">
									<text class="icon-text">💡</text>
								</view>
								<text class="suggestion-text">{{ suggestion }}</text>
							</view>
						</view>
					</view>
				</app-card>
			</view>
		</view>
	</gradient-background>
</template>

<script>
import GradientBackground from '@/components/gradient-background/gradient-background.vue'
import AppCard from '@/components/app-card/app-card.vue'
import AppButton from '@/components/app-button/app-button.vue'
import ToolsService from '@/utils/tools-service.js'
import UserStateManager from '@/utils/user-state-manager.js'

export default {
	name: 'FindPersonToolPage',
	components: {
		GradientBackground,
		AppCard,
		AppButton
	},
	data() {
		return {
			params: {
				personName: '',
				relation: '',
				lostDate: '',
				lastLocation: '',
				otherInfo: ''
			},
			findPersonResult: null,
			loading: false,
			toolsService: null,
			relationIndex: 0,
			relationTypes: [
				'家人', '朋友', '同事', '同学', 
				'邻居', '亲戚', '恋人', '其他'
			]
		}
	},
	computed: {
		canQuery() {
			return this.params.personName.trim().length > 0 && 
				   this.params.relation && this.params.lostDate
		},
		
		displayRelation() {
			return this.params.relation || '选择关系'
		},
		
		displayLostDate() {
			return this.params.lostDate || '选择失联日期'
		}
	},
	onLoad() {
		this.initData()
	},
	methods: {
		initData() {
			this.toolsService = new ToolsService()
			
			// 自动填写用户信息
			this.autoFillUserInfo()
		},
		
		/**
		 * 自动填写用户信息
		 */
		autoFillUserInfo() {
			const autoFillInfo = UserStateManager.getAutoFillInfo()
			if (autoFillInfo) {
				uni.showToast({
					title: '已为您登录，开始占卜！',
					icon: 'none',
					duration: 2000
				})
			} else {
				setTimeout(() => {
					UserStateManager.promptCompleteInfo()
				}, 1000)
			}
		},
		
		goBack() {
			uni.navigateBack()
		},
		
		onRelationChange(e) {
			this.relationIndex = e.detail.value
			this.params.relation = this.relationTypes[e.detail.value]
		},
		
		onLostDateChange(e) {
			this.params.lostDate = e.detail.value
		},
		
		async queryFindPerson() {
			if (!this.canQuery) {
				uni.showToast({
					title: '请完善寻人信息',
					icon: 'none'
				})
				return
			}
			
			this.loading = true
			
			try {
				const result = await this.toolsService.getFindPersonInfo(this.params)
				
				if (result.success) {
					this.findPersonResult = result.data
					uni.showToast({
						title: '占卜完成',
						icon: 'success'
					})
				} else {
					throw new Error(result.message || '占卜失败')
				}
			} catch (error) {
				console.error('寻人占卜失败:', error)
				uni.showToast({
					title: '占卜失败，请重试',
					icon: 'none'
				})
			} finally {
				this.loading = false
			}
		}
	}
}
</script>

<style lang="scss" scoped>
.container {
	min-height: 100vh;
	padding: 32rpx;
}

.content {
	padding-top: 40rpx;
}

.page-header {
	display: flex;
	align-items: center;
	margin-bottom: 40rpx;
	
	.back-button {
		width: 80rpx;
		height: 80rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		background-color: rgba(255, 255, 255, 0.9);
		border-radius: 40rpx;
		margin-right: 24rpx;
		box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
		
		.back-icon {
			font-size: 32rpx;
			color: #000000;
			font-weight: bold;
		}
	}
	
	.header-info {
		flex: 1;
		
		.tool-icon {
			font-size: 64rpx;
			margin-bottom: 8rpx;
		}
		
		.tool-title {
			display: block;
			font-size: 40rpx;
			font-weight: bold;
			color: #000000;
			margin-bottom: 8rpx;
		}
		
		.tool-subtitle {
			display: block;
			font-size: 26rpx;
			color: rgba(0, 0, 0, 0.7);
		}
	}
}

.input-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.form-item {
		margin-bottom: 32rpx;
		
		&:last-child {
			margin-bottom: 0;
		}
		
		.form-label {
			display: block;
			font-size: 28rpx;
			color: #000000;
			margin-bottom: 16rpx;
		}
		
		.input-field {
			width: 100%;
			height: 88rpx;
			padding: 0 24rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
		}
		
		.relation-picker,
		.date-picker {
			.picker-display {
				display: flex;
				align-items: center;
				justify-content: space-between;
				height: 88rpx;
				padding: 0 24rpx;
				border: 2rpx solid #f0f0f0;
				border-radius: 12rpx;
				background-color: #fafafa;
				
				.picker-text {
					font-size: 28rpx;
					color: #000000;
				}
				
				.picker-arrow {
					font-size: 24rpx;
					color: rgba(0, 0, 0, 0.6);
				}
			}
		}
		
		.location-input,
		.info-input {
			width: 100%;
			min-height: 120rpx;
			padding: 16rpx;
			border: 2rpx solid #f0f0f0;
			border-radius: 12rpx;
			font-size: 28rpx;
			background-color: #fafafa;
			line-height: 1.5;
		}
	}
}

.action-section {
	margin: 48rpx 0;
}

.result-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.find-person-analysis {
		.overall-result {
			display: flex;
			align-items: center;
			padding: 32rpx;
			background: linear-gradient(135deg, #FF5722 0%, #E64A19 100%);
			border-radius: 16rpx;
			
			.result-score {
				display: flex;
				flex-direction: column;
				align-items: center;
				margin-right: 32rpx;
				
				.score-circle {
					width: 120rpx;
					height: 120rpx;
					border: 4rpx solid rgba(255, 255, 255, 0.3);
					border-radius: 50%;
					display: flex;
					flex-direction: column;
					align-items: center;
					justify-content: center;
					margin-bottom: 12rpx;
					
					.score-text {
						font-size: 36rpx;
						font-weight: bold;
						color: white;
					}
					
					.score-unit {
						font-size: 20rpx;
						color: rgba(255, 255, 255, 0.8);
					}
				}
				
				.score-level {
					font-size: 24rpx;
					color: white;
					font-weight: 500;
				}
			}
			
			.result-desc {
				flex: 1;
				
				.desc-text {
					font-size: 28rpx;
					color: white;
					line-height: 1.6;
				}
			}
		}
	}
}

.direction-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.direction-analysis {
		display: flex;
		flex-direction: column;
		gap: 16rpx;
		
		.direction-item {
			display: flex;
			flex-direction: column;
			align-items: center;
			padding: 24rpx;
			border-radius: 12rpx;
			
			&.primary {
				background: linear-gradient(135deg, #4CAF50 0%, #388E3C 100%);
				color: white;
			}
			
			&.secondary {
				background: linear-gradient(135deg, #2196F3 0%, #1976D2 100%);
				color: white;
			}
			
			&.avoid {
				background: linear-gradient(135deg, #F44336 0%, #D32F2F 100%);
				color: white;
			}
			
			.direction-label {
				font-size: 24rpx;
				margin-bottom: 8rpx;
				opacity: 0.8;
			}
			
			.direction-value {
				font-size: 32rpx;
				font-weight: bold;
				margin-bottom: 8rpx;
			}
			
			.direction-desc {
				font-size: 22rpx;
				opacity: 0.9;
			}
		}
	}
}

.time-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.time-analysis {
		display: flex;
		flex-direction: column;
		gap: 16rpx;
		
		.time-item {
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx;
			border-radius: 12rpx;
			
			&.best {
				background-color: #e8f5e8;
				border-left: 4rpx solid #4CAF50;
			}
			
			&.possible {
				background-color: #e3f2fd;
				border-left: 4rpx solid #2196F3;
			}
			
			&.difficult {
				background-color: #ffebee;
				border-left: 4rpx solid #F44336;
			}
			
			.time-label {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.7);
			}
			
			.time-value {
				font-size: 28rpx;
				font-weight: 500;
				color: #000000;
			}
		}
	}
}

.location-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.location-analysis {
		.location-info {
			margin-bottom: 24rpx;
			padding: 20rpx;
			background-color: #f8f9fa;
			border-radius: 12rpx;
			
			.location-type {
				display: block;
				font-size: 28rpx;
				font-weight: 500;
				color: #FF5722;
				margin-bottom: 8rpx;
			}
			
			.location-description {
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.8);
				line-height: 1.5;
			}
		}
		
		.location-keywords {
			.keywords-label {
				display: block;
				font-size: 26rpx;
				color: rgba(0, 0, 0, 0.7);
				margin-bottom: 12rpx;
			}
			
			.keywords-list {
				display: flex;
				flex-wrap: wrap;
				gap: 8rpx;
				
				.keyword-item {
					padding: 8rpx 16rpx;
					background-color: #FF5722;
					color: white;
					border-radius: 20rpx;
					font-size: 24rpx;
				}
			}
		}
	}
}

.suggestions-section {
	.section-title {
		display: block;
		font-size: 32rpx;
		font-weight: 500;
		color: #000000;
		margin-bottom: 32rpx;
	}
	
	.suggestions-list {
		.suggestion-item {
			display: flex;
			align-items: flex-start;
			margin-bottom: 20rpx;
			
			&:last-child {
				margin-bottom: 0;
			}
			
			.suggestion-icon {
				margin-right: 16rpx;
				margin-top: 4rpx;
				
				.icon-text {
					font-size: 32rpx;
				}
			}
			
			.suggestion-text {
				flex: 1;
				font-size: 28rpx;
				color: #000000;
				line-height: 1.6;
			}
		}
	}
}
</style>